package random.fox.p2p.interactors;

import android.text.TextUtils;
import android.util.Log;

import com.android.volley.VolleyError;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import random.fox.p2p.App;
import random.fox.p2p.api.ApiClient;
import random.fox.p2p.bean.BeanModule;
import random.fox.p2p.bean.greendao.User;

/**
 * Created by wq on 15/3/19.
 */
public class RegisterInteractorImpl implements RegisterInteractor{
    @Inject User user;
    App app;
    ApiClient apiClient;
    String TAG = getClass().getSimpleName();

    public RegisterInteractorImpl(App app){
        this.app = app;
        apiClient = app.getApiClient();
        app.createScopedGraph(new BeanModule()).inject(this);
    }

    @Override
    public void savePhoneNumber(final String phoneNumber, final OnFinishRegisterListener listener) {
        try {
            apiClient.getCode(phoneNumber, new ApiClient.CallBack<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    String code = jsonObject.optString("code");
                    listener.onGetCode(code);
                    user.setPhoneNumber(phoneNumber);
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error);
                }
            });

        }catch (Exception e){
            Log.e(TAG, e.toString());
            listener.onError(e);
        }
    }

    @Override
    public void savePassword(String password, final OnFinishRegisterListener listener) {
        try {
            apiClient.setPassword(user.getPhoneNumber(),password,new ApiClient.CallBack<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    String uid = o.optString("uid");
                    user.setUid(uid);
                    listener.onGetUid(uid);
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    listener.onError(error);
                }
            });
        }catch (Exception e){
            listener.onError(e);
            Log.e(TAG, e.toString());
        }
    }


    @Override
    public void setInfo(final String nickName, File image, final String birthday, final int sex,
                        final String school, final String city, final OnFinishSetInfoListener listener) {
        Map<String,String> params = new HashMap<>();
        params.put("uid",user.getUid());
        if (!TextUtils.isEmpty(nickName))
            params.put("nickname",nickName);
        if (image!=null && image.exists()){
            params.put("img",image.getName());
        }
        if (!TextUtils.isEmpty(birthday))
            params.put("birthday",birthday);
        if (!TextUtils.isEmpty(school))
            params.put("school",school);
        if (!TextUtils.isEmpty(city))
            params.put("city",city);

        apiClient.setInfo(params,image,new ApiClient.CallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                listener.onSuccess();
                user.setBirthday(birthday);
                user.setCity(city);
                user.setNickname(nickName);
                user.setSchool(school);
                user.setSex(sex);
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG,error.toString());
                //TODO 错误代码的定义还未开始
                listener.onError(1);//暂时用1
            }
        });
    }
}
